//
// Created by tony3 on 10/23/2018.
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


struct filter{
    int buff[100];
    int input;
    short type;

    struct lowpass{
        short forget_rate; //0-100
        int memory;
    }low_pass;

    struct highpass{

    }hogh_pass;
};

enum filter_type{
    low_pass,
    high_pass
};

struct filter filter1;//new a filter

int low_pass_filter(struct filter *obj_filter,int data){
    obj_filter->low_pass.memory=((100-obj_filter->low_pass.forget_rate)*obj_filter->low_pass.memory+obj_filter->low_pass.forget_rate*data);
    return obj_filter->low_pass.memory;
}

int high_pass_filter(struct filter *obj_filter,int data){

}


void filter_type_def(struct filter *obj_filter, char *type){//high or low
    if(type=="high"||type=="HIGH") obj_filter->type=high_pass;
    if(type=="low"||type=="low") obj_filter->type=low_pass;
    else {printf("Filter type not def\n");}
}


void low_pass_init(struct filter *obj_filter, short forget_rate){
    obj_filter->low_pass.forget_rate=forget_rate;
    obj_filter->low_pass.memory=0;
}


void filter_solve(struct filter *obj_filter,int data){//use this in your thread!
    switch(obj_filter->type){
        case low_pass:
            obj_filter->input=low_pass_filter(obj_filter,data);
            break;

        case high_pass:
            obj_filter->input=high_pass_filter(obj_filter,data);
            break;

        default:
            printf("Error: No filter type defined\n");
            break;
    }
}


void filter_data_update(struct filter *obj_filter, int data) {//not useful
    int i;
    for(i=98;i>0;i--){
        obj_filter->buff[i+1]=obj_filter->buff[i];
    }//update data array;
    obj_filter->buff[0]=data;
}
